package com.moter.demo01.requirements.socket_threadpool;


import java.io.*;

import java.net.Socket;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;


public class Client {
    private Socket client = null;
    private DataOutputStream writer = null;
    private BufferedReader reader = null;

    public void initClient() {

        try {
            client = new Socket("localhost", 9090);

            System.out.println("连接到服务器");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        try {
            Scanner scanner = new Scanner(System.in);
            String s = "";
            while (!s.equals("quit")) {
                reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
                String msg = reader.readLine();
                System.out.println(msg);
                writer = new DataOutputStream(client.getOutputStream());

                s = scanner.nextLine();
                writer.write(s.getBytes());
                writer.flush();
            }

            writer.close();
            reader.close();
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.initClient();
        client.start();
    }
}
